baselib.report(0x00, "TD4.8.3 beginning.")
-- 初始化操作
busilib.SetOrientation(1)  -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
-- 第一步:CVS presents 1 CC pin in the vRa range and one in rRd like a VCONN powered Accessory
-- CVS将一个CC引脚设置在vRa范围内，另一个引脚设置在rRd范围内，模拟VCONN供电的配件
busilib.SetSwitchState(SW.CC1_RD, 1)  -- 将CC1_RD开关打开
busilib.SetSwitchState(SW.CC2_RA, 1)  -- 将CC2_RA开关打开
-- 第二步:CVS verifies the PUT transitions to Unattached.SRC within tDRP – (dcSRC.DRP x tDRP). [TD.4.8.3.V.1]
-- CVS验证PUT在tDRP – (dcSRC.DRP x tDRP)时间内转换到Unattached.SRC状态
baselib.delay(30)  -- 等待tDRP – (dcSRC.DRP x tDRP)时间
local VCC1 = busilib.GetADC(ADC.CC1, 4)
if VCC1 > 200 then
    baselib.report(0x00, "TD4.8.3.V.1 pass.")
else
    baselib.report(0x00, "TD4.8.3.V.1 fail.")
end
-- 第三步:If VIF field Type_C_Implements_Try_SNK is YES:
if Type_C_Implements_Try_SNK then
    -- 3.a步:CVS verifies PUT transitions to Try.SNK within tCCDebounce [TD.4.8.3.V.2]
    baselib.delay(200)  -- 等待tCCDebounce时间
    -- 这个状态怎么验证？
    -- 3.b步:CVS verifies PUT transitions to TryWait.SRC within tTryCCDebounce + tDRPTry after entering Try.SNK  [TD.4.8.3.V.3]
    -- 这个状态怎么验证？
end


e. If the PUT is a PUT_V:
i. PUT sources Vconn within tVconnON [TD.4.8.3.V.8]
ii. If PUT has VIF field Attempts_Discov_SOP set to YES:
01 PUT initiates Alternate Mode discovery with Structured 
VDMs [TD.4.8.3.V.9]
-- 第四步:CVS verifies that PUT transitions to Attached.SRC
-- 4.a步:If VIF field Type_C_Implements_Try_SNK is YES: 
if Type_C_Implements_Try_SNK then
    -- PUT sources Vbus after tTryCCDebounce and before tTryCCDebounce + tVbusON expires [TD.4.8.3.V.4]
    -- PUT在tTryCCDebounce后且在tTryCCDebounce + tVbusON到期前提供Vbus
    baselib.delay(295)  -- 等待tTryCCDebounce(20) + tVbusON(275)时间
    if cstmlib.VBusVerified(1) then 
        baselib.report(0x00, "TD4.8.3.V.4 pass.")
    else
        baselib.report(0x00, "TD4.8.3.V.4 fail.")
    end
-- 4.b步:If VIF field Type_C_Implements_Try_SNK is NO:
else
    -- PUT sources VBUS after tCCDebounce and before tCCDebounce + tVBUSON expires [TD.4.8.3.V.5]
    -- PUT在tCCDebounce后且在tCCDebounce + tVBUSON到期前提供VBUS
    baselib.delay(475)  -- 等待tCCDebounce(200) + tVBUSON时间
    if cstmlib.VBusVerified(1) then 
        baselib.report(0x00, "TD4.8.3.V.5 pass.")
    else
        baselib.report(0x00, "TD4.8.3.V.5 fail.")
    end
end
-- 4.c步:PUT advertises VBUS capability over Rp [TD.4.8.3.V.6]
-- 之前有讨论过通过检查CC1上电压判断PUT能力
-- 4.d步:If the PUT supports PD:PUT and CVS negotiate an explicit contract [TD.4.8.3.V.7]
if PUT_supports_PD then
    tcpclib.SetPermission(1, 2)
    PN_result = tcpclib.PresetPowerNegotiation(1, 500)  -- timeout is 500ms
    if PN_result == LuaOk then
        baselib.report(0x00, "TD4.8.3.V.7 pass.")
    else
        baselib.report(0x00, "TD4.8.3.V.7 fail.")
    end
end
-- 4.e步:如果PUT是PUT_V
if PUT_V then
    -- PUT在tVconnON时间内提供Vconn
    baselib.delay(100)  -- 等待tVconnON时间
    local VCONN = busilib.GetADC(ADC.VCONN, 4)
    if VCONN > 0 then
        baselib.report(0x00, "TD4.8.3.V.8 pass.")
    else
        baselib.report(0x00, "TD4.8.3.V.8 fail.")
    end
    -- 如果PUT的VIF字段Attempts_Discov_SOP设置为YES
    if Attempts_Discov_SOP then
        -- PUT使用结构化VDM启动替代模式发现
        if busilib.InitiateAltModeDiscovery() then
            baselib.report(0x00, "TD4.8.3.V.9 pass.")
        else
            baselib.report(0x00, "TD4.8.3.V.9 fail.")
        end
    end
end

-- 第五步:CVS transitions to Disabled
busilib.setUsbCcs(CCS.Disabled)

-- 第六步:CVS verifies the PUT transitions to Unattached.SNK within tSRCDisconnect
baselib.delay(650)  -- 等待tSRCDisconnect时间
if busilib.GetState() == "Unattached.SNK" then
    baselib.report(0x00, "TD4.8.3.V.10 pass.")
else
    baselib.report(0x00, "TD4.8.3.V.10 fail.")
end

-- 6.a步:PUT stops sourcing VCONN within tVCONNOFF
baselib.delay(100)  -- 等待tVCONNOFF时间
local VCONN = busilib.GetADC(ADC.VCONN, 4)
if VCONN == 0 then
    baselib.report(0x00, "TD4.8.3.V.10 pass.")
else
    baselib.report(0x00, "TD4.8.3.V.10 fail.")
end

-- 6.b步:PUT stops sourcing Vbus within tVbusOFF
baselib.delay(650)  -- 等待tVbusOFF时间
local VBUS = busilib.GetADC(ADC.VBUS, 4)
if VBUS == 0 then
    baselib.report(0x00, "TD4.8.3.V.11 pass.")
else
    baselib.report(0x00, "TD4.8.3.V.11 fail.")
end

-- 6.c步:PUT attaches Rd on its CCs
local VCC1 = busilib.GetADC(ADC.CC1, 4)
local VCC2 = busilib.GetADC(ADC.CC2, 4)
if VCC1 < 200 and VCC2 < 200 then
    baselib.report(0x00, "TD4.8.3.V.12 pass.")
else
    baselib.report(0x00, "TD4.8.3.V.12 fail.")
end

baselib.report(0x00, "TD4.8.3 completed.")
baselib.report(0x07, "\x04")